package com.ibiaoke.customer.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.PtOpenGroup;
import com.ibiaoke.customer.entity.PtOpenGroupExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ibiaoke.customer.dao.PtOpenGroupDao;

import java.util.Date;
import java.util.List;

@Service("openGroupService")
public class OpenGroupServiceImpl implements OpenGroupService{

    @Autowired
    private PtOpenGroupDao ptOpenGroupDao;

    @Override
    public PtOpenGroup savePtOpenGroup(PtOpenGroup ptOpenGroup) {
        ptOpenGroupDao.insertSelective(ptOpenGroup);
        return ptOpenGroup;
    }

    @Override
    public List<PtOpenGroup> openGroupList(Integer ptGoodsId, Page page) {
        AssertUtils.notNull(ptGoodsId,"拼团商品Id不能为空");
        return ptOpenGroupDao.openGroupList(ptGoodsId,page);
    }

    @Override
    public List<PtOpenGroup> getPtOpenGroup(Integer id) {
        return ptOpenGroupDao.getOpenGroup(id);
    }

    @Override
    public PtOpenGroup getPtOpenGroupById(Integer id) {
        AssertUtils.notNull(id,"开团人Id");
        return ptOpenGroupDao.getPtOpenGroupById(id);
    }

    @Override
    public PtOpenGroup getPtOpenGroupByKid(Integer kid) {
       return  ptOpenGroupDao.getPtOpenGroupByKid(kid);
    }

    @Override
    public Integer updatePtOpenGroupCount(Integer id, Integer num) {
        return ptOpenGroupDao.updatePtOpenGroupCount(id,num);
    }

    @Override
    public Integer updatePayTypeAndPayMoney(Integer id,Integer payType,Double payMoney) {
        return ptOpenGroupDao.updatePayTypeAndPayMoney(id,payType,payMoney);
    }

    @Override
    public PtOpenGroup checkPtOpenGroup(Integer customerId, Integer ptGoodsId, Integer kid) {
        AssertUtils.notNull(customerId,"用户Id不能为空");
        AssertUtils.notNull(ptGoodsId,"拼团商品Id不能为空");
        AssertUtils.notNull(kid,"开团Id不能为空");

        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria().andUserIdEqualTo(customerId).andPtGoodsIdEqualTo(ptGoodsId).andKidEqualTo(kid);
        return ptOpenGroupDao.getByExample(example);
    }

    @Override
    public Integer updateOpenGroupStatus(Integer id, Integer status) {
        return  ptOpenGroupDao.updateOpenGroupStatus(id,status);
    }

    @Override
    public Integer deleteOpenGroup(Integer kid, Integer isDelete) {
        return ptOpenGroupDao.deleteOpenGroup(kid,isDelete);
    }

    @Override
    public PtOpenGroup checkPtOpenGroupIsOk(Integer kid) {
        AssertUtils.notNull(kid,"开团Id不能为空");

        return ptOpenGroupDao.selectByPrimaryKey(kid);
    }

    @Override
    public PtOpenGroup getPtOpenGroupByOpenCode(Integer kid) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria().andKidEqualTo(kid).andStatusEqualTo(CommonConsts.YES).andIsOpenEqualTo(CommonConsts.YES);
        return ptOpenGroupDao.getByExample(example);
    }

    @Override
    public PtOpenGroup getPtOpenGroupByOpenCodeAndUserIdAndPtGoodsId(String openCode, Customer customer, Integer ptGoodsId) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria()
                .andOpenCodeEqualTo(openCode)
                .andUserIdEqualTo(customer.getCustomerId())
                .andStatusEqualTo(CommonConsts.YES)
                .andPtGoodsIdEqualTo(ptGoodsId);
        return ptOpenGroupDao.getByExample(example);
    }

    @Override
    public List<PtOpenGroup> getPtOpenGroupByOpenCodeAndPtGoodsId(String openCode, Integer ptGoodsId) {
        AssertUtils.notBlank(openCode,"开团编码不能为空");
        AssertUtils.notNull(ptGoodsId,"拼团商品Id不能为空");
        return ptOpenGroupDao.getPtOpenGroupByOpenCodeAndPtGoodsId(openCode,ptGoodsId);
    }

    @Override
    public PtOpenGroup checkOpenGroup(String OpenCode, Integer PtGoodsId, Integer customerId) {
        AssertUtils.notBlank(OpenCode,"开团编码不能为空");
        AssertUtils.notNull(PtGoodsId,"商品Id不能为空");
        AssertUtils.notNull(customerId,"用户Id不能为空");
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria().andOpenCodeEqualTo(OpenCode).andPtGoodsIdEqualTo(PtGoodsId).andUserIdEqualTo(customerId);
        return ptOpenGroupDao.getByExample(example);
    }

    @Override
    public List<PtOpenGroup> getPtOpenGroupListByOpenCode(String openCode) {
        return ptOpenGroupDao.getPtOpenGroupListByOpenCode(openCode);
    }

    @Override
    public Integer updateIsPay(Integer Kid, Integer status) {
        return ptOpenGroupDao.updateIsPay(Kid,status);
    }

    @Override
    public List<PtOpenGroup> bossOpenGroupList(Integer ptGoodsId, Page page) {
        return ptOpenGroupDao.bossOpenGroupList(ptGoodsId,page);
    }

    @Override
    public List<PtOpenGroup> listOpenGroupByOpenCode(String openCode, Page page) {
        AssertUtils.notBlank(openCode,"开团码不能为空");
        return ptOpenGroupDao.listOpenGroupByOpenCode(openCode,page);
    }
}
